#!/bin/bash

. $OPENARS_HOME/common/debug.h
. $OPENARS_HOME/common/functions.h

>$OUTFILE

repository="$REPOSITORY_PATH/configs"
if [ ! -d "$repository" ]; then
    ERR "not find repository dir"
    exit 1
fi

scheme_zabbix=`echo $CONNECT_ZABBIX | awk -F"/" '{print $1}'`

ora_lst=`$ORACLE_HOME/bin/sqlplus -s /NOLOG <<END
set $SQL_PARAM
connect $CONNECT_CMDB
select distinct s.host from hosts s, $scheme_zabbix.hosts h,bases_tmp b where s.host=h.host and s.host=b.host_name and s.OS='Unix' and s.PROD='PROD' and s.control like 'YES' and h.available=1 order by host;
exit;
END`
if [ $? -ne 0 ]; then
    ERR "sqlplus ERROR: $ora_lst"
    exit 1
fi

con_lst=`$ORACLE_HOME/bin/sqlplus -s /NOLOG  <<END
set $SQL_PARAM
connect $CONNECT_CMDB
select s.host from hosts s, $scheme_zabbix.hosts h where s.host=h.host and s.os='Unix' and s.prod='PROD' and s.control='YES' and h.available=1;
exit;
END`
if [ $? -ne 0 ]; then
    ERR "sqlplus ERROR: $con_lst"
    exit 1
fi

INF "Check `echo "$con_lst" | awk 'END{print NR}'` servers. `echo "$ora_lst" | awk 'END{print NR}'` have Oracle"

#command for ssh
cksum1="/usr/bin/cksum /etc/zabbix/zabbix_agentd.conf | awk '{print \$1}'"
cksum2="/usr/bin/cksum /etc/zabbix/.localize | awk '{print \$1}'"
cksum3="/usr/bin/cksum /etc/zabbix/.conn | awk '{print \$1}'"

for server in $con_lst
do 
(
 ck1_loc=`/usr/bin/cksum $repository/$server/zabbix_agentd.conf | awk '{print $1}'`
 ck2_loc=`/usr/bin/cksum $repository/$server/.localize | awk '{print $1}'`

 SSH $USER_ZABBIX@$server "$cksum1; $cksum2" result
 [ "$?" -ne 0 ] && continue
 ck1_ssh=`echo $result | awk '{print $1}'`
 ck2_ssh=`echo $result | awk '{print $2}'`

 [ -z "$ck1_loc" ] && ERR "not get $server/zabbix_agentd.conf cksumm in repository"    
 [ -z "$ck2_loc" ] && ERR "not get $server/.localize cksumm in repository"    
 [ -z "$ck1_ssh" ] && ERR "not get zabbix_agentd.conf cksumm on $server"    
 [ -z "$ck2_ssh" ] && ERR "not get .localize cksumm on $server"    
 [ "$ck1_ssh" != "$ck1_loc" ] && ERR "Error zabbix_agentd.conf cksumm in repository or $server!"
 [ "$ck2_ssh" != "$ck2_loc" ] && ERR "Error .localize cksumm in repository or $server!"
) &
done
for server in $ora_lst
do 
(
 ck3_loc=`/usr/bin/cksum $repository/$server/.conn | awk '{print $1}'`

 SSH $USER_ZABBIX@$server "$cksum3" result
 [ "$?" -ne 0 ] && continue

 [ -z "$ck3_loc" ] && ERR "not get $server/.conn cksumm in repository"    
 [ -z "$result" ] && ERR "not get .conn cksumm on $server"    
 [ "$result" != "$ck3_loc" ] && ERR "Error .conn cksumm in repository or $server!"
) &
done

wait
exit 0;

